//
//  RBGHomeController.m
//  BateauGenerique
//
//  Created by Kevin De Jesus Ferreira on 20/05/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGHomeController.h"
#import "RBGNavigationBar.h"
#import "RBGCreditsController.h"
#import "RBGAppDelegate.h"
#import "REPagedScrollView.h"
#import "UIImageView+WebCache.h"

@interface RBGHomeController ()<UITextViewDelegate>{
    RBGAppDelegate *appDelegate;
    NSMutableArray *imagesPager;
    REPagedScrollView *pager;
    NSInteger page;
    bool first;
    NSTimer * timer;
}

@end

@implementation RBGHomeController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self charger];
    [self remplir];
    [self positionnerVue];
    [self chargerCouleur];
    [self ajouterImages];
    
}

-(void) viewDidDisappear:(BOOL)animated
{
    [timer invalidate];
    timer = nil;
}

-(void) viewWillAppear:(BOOL)animated
{
    
    [[UIApplication sharedApplication] setStatusBarHidden:NO];

    timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(imageSuivante) userInfo:nil repeats:YES] ;
    
    [timer fire];
}

-(void) charger
{
    
    first = YES;

    appDelegate = (RBGAppDelegate *)[[UIApplication sharedApplication] delegate];
    imagesPager = [NSMutableArray array];
    
    for(NSString * url in appDelegate.donnees.parametres.imagesSlide){
        [imagesPager addObject:url];
        
    }
    

    // On rend le text view scrollable
    [self.descriptionSociete setUserInteractionEnabled:YES];
    [self.descriptionSociete setScrollEnabled:YES];
    [self.descriptionSociete setEditable:NO];
    
}

-(void) remplir
{
    [self.logo setImageWithURL:[NSURL URLWithString:appDelegate.donnees.parametres.imageLogo]];
    [self.fond setImageWithURL:[NSURL URLWithString:appDelegate.donnees.parametres.imageFond]];
    [self.descriptionSociete setText:appDelegate.donnees.parametres.texteIntro];
}

-(void)ajouterImages
{
    CGRect pagerFrame = self.pager.frame;
    
    pager = [[REPagedScrollView alloc] initWithFrame:pagerFrame];
    
    pager.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pager.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    
    [self.view addSubview:pager];
    
    
    //ajout des images
    
    for(NSString *url in imagesPager){
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, pagerFrame.size.width, pagerFrame.size.height)];
        
        [imageView setImageWithURL:[NSURL URLWithString:url]];
        [imageView setContentMode:UIViewContentModeScaleAspectFit];
        
        
        [pager addPage:imageView];
    }
    
    timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(imageSuivante) userInfo:nil repeats:YES] ;
    
    [timer fire];
}

-(void)imageSuivante
{
    
    if(!first)
        page++;
    
    if(page > [pager.pages count]){
        page = 0;
        first = YES;
    }else{
        first = NO;
    }
    
    [pager scrollToPageWithIndex:page animated:YES];
    
}


-(void) positionnerVue
{
    [self.logo setFrame:CGRectMake(120, 10, 100, 80)];
    [self.pager setFrame:CGRectMake(0, self.logo.frame.origin.y + self.logo.frame.size.height, 320, self.pager.frame.size.height+50)];
    [self.descriptionSociete setFrame:CGRectMake(10, self.pager.frame.origin.y+self.pager.frame.size.height, 310, 280)];
    
}

-(void) chargerCouleur
{
    self.navigationController.navigationBarHidden = true;
    
    self.descriptionSociete.backgroundColor = [UIColor clearColor];
    
    self.descriptionSociete.textColor = appDelegate.donnees.parametres.fontColorDeux;
}
@end
